﻿function CGrid(opts) {
    var self = this;
    this.container = "";
    this.options = {};
    $.extend(self.options, opts);
    /* Private */
    var fn_ContextMenu = function () {
        var colModel = self.GetColumnModel();
        var colNames = self.GetColumnName();
        var itemContextMenu = {};
        if (colModel) {
            $.each(colModel, function (index, item) {
                if (item.hidedlg != true) {
                    var disabled = false;
                    if (item.frozen == true)
                        disabled = true;
                    var selected = item.hidden ? false : true;
                    var caption = colNames[index];
                    itemContextMenu[item.name] = {
                        name: caption,
                        type: 'checkbox',
                        selected: selected,
                        disabled: disabled,
                        events: {
                            change: function (e) {
                                var _colName = $(this).attr("name").replace("context-menu-input-", "");
                                if ($(this).is(":checked"))
                                    self.ShowColumn(_colName);
                                else
                                    self.HideColumn(_colName);
                            }
                        }
                    };
                }
            });
        }
        var options = {
            selector: '.ui-jqgrid-htable',
            items: itemContextMenu,
        };
        var contextMenu = new CContextMenu(options);
        contextMenu.Init(".ui-jqgrid-htable");
    };
    /* Public Methods */
    this.Init = function (container) {
        self.container = container;
        var options = self.options;
        $(container).jqGrid(options);
        /* Create Context Menu Column Header */
        fn_ContextMenu();
        if ($(".ui-layout-center")) {
            $(window).bind("resize", function () {
                var new_height = $(".ui-layout-center").height() - 90;
                self.SetHeight(new_height);
                var new_rowNum = Math.floor(new_height / 23);
                self.setGridParam({ rowNum: new_rowNum });
                self.ReloadGrid();
            }).trigger("resize");
        }
    };
    this.NavGrid = function (paperOpts) {
        var options = {
            pager: self.options.pager || "",
            edit: false,
            add: false,
            del: false,
            search: false
        };
        $.extend(options, paperOpts);
        var container = self.container;
        $(container).jqGrid('navGrid', options.pager, options);
    };
    this.ReloadGrid = function () {
        var container = self.container;
        $(container).trigger("reloadGrid");
    };
    this.FrozenColumns = function () {
        var container = self.container;
        $(container).jqGrid("setFrozenColumns");
    };
    this.SelectRow = function () {
        var container = self.container;
        var rowID = $(container).jqGrid('getGridParam', 'selrow');
        return $(container).getRowData(rowID);
    };
    this.SelectArrRow = function () {
        var container = self.container;
        var arrRowID = $(container).jqGrid('getGridParam', 'selarrrow');
        var ret = [];
        $.each(arrRowID, function (index, rowID) {
            var rowData = $(container).getRowData(rowID);
            ret.push(rowData);
        });
        return ret;
    };
    this.SetHeight = function (height) {
        var container = self.container;
        var rowID = $(container).jqGrid('setGridHeight', height);
    };
    this.GetColumnName = function () {
        var container = self.container;
        return $(container).jqGrid('getGridParam', 'colNames');
    };
    this.GetColumnModel = function () {
        var container = self.container;
        return $(container).jqGrid('getGridParam', 'colModel');
    };
    this.HideColumn = function (colName) {
        var container = self.container;
        $(container).hideCol(colName);
    };
    this.ShowColumn = function (colName) {
        var container = self.container;
        $(container).showCol(colName);
    };
    this.setGridParam = function (param) {
        var container = self.container;
        $(container).setGridParam(param);
    };
}